-- LictorM
-- Create by chendh
-- 扈从模块

module("LictorM", package.seeall);

local lictorPets = {}

-- 获取当前出战的灵魂链接
function getLictorPets()
    return ME.user.dbase:query("lictor_pets") or {};
end

-- 派遣扈从
function dispatchLictor(lictorIds, dungeonId)
    local petIds = {};
    local isClear = true;
    for _, classId in pairs(lictorIds) do
        if not canDispatch(classId, 3) then
            table.insert(petIds, 0);
        else
            isClear = false;
            table.insert(petIds, classId);
        end
    end

    ME.user.dbase:set("lictor_pets", petIds);

    -- 刷新下属性
    PropM.refresh(ME.user)

    -- 抛出事件
    EventMgr.fire(event.DISPATCH_LICTOR, { ["petIds"] = petIds,
        ["dungeonId"] = dungeonId, ["isClear"] = isClear, });

    return true;
end

-- 扈从数量限制
function lictorLimit()
    if AlchemyWorkshopM.getStrengthenLevel(21) > 0 then
        return 2;
    elseif AlchemyWorkshopM.getStrengthenLevel(20) > 0 then
        return 1;
    end

    return 0;
end

-- 扈从阶位要求
function lictorRankRequest()
    -- 工坊的 灵魂链接装置 功能还没做，默认要求4阶
    return 4;
end

-- 能否派遣扈从
function canDispatch(classId, rankRequest)
    local activePet = ME.user.dbase:query("active_pet");

    -- 不能是出战的宠物
    if (classId == activePet) then
        return false;
    end

    -- 必须是已激活的宠物
    if (PetM.isActivated(classId) == false) then
        return false;
    end

    -- 必须跟出战宠物同阵营
    if (not IsInTable(classId, CampM.getCampPets(activePet))) then
        return false;
    end

    if TrainingM.isUpgrading(classId) or
        AwakeM.isCarving(classId) then
        return false;
    end

    -- 判断阶位是否符合条件
    local pet = PetM.getMyPet(classId);

--    if pet:getRank() < rankRequest then
--        return false;
--    end

    return true;
end

-- 获取所有扈从附加的属性
function fetchAllProps()
    local lictorPets = ME.user.dbase:query("lictor_pets");
    local props = {};

    if not lictorPets then
        return props;
    end

    for i=1, #lictorPets do
        local classId = lictorPets[i];
        local pet = PetM.getMyPet(classId);

        if pet then
            local prop = fetchProp(pet);
            for _, p in pairs(prop) do
                table.insert(props,p);
            end
        end
    end

    return props;
end

-- 获取指定扈从附加的属性
function fetchProp(pet)
    local prop = {};
    local skillId = pet:getSpecial();
    skillId = FormulaM.invoke("CALC_FINAL_SKILL", pet, skillId);

    local row = SkillM.getRow(skillId);

    if not row then
        trace("LictorM", "无法附加扈从的专属技能，技能(%s)不存在", tostring(skillId));
        return {};
    end

    -- 如果是主动技能
    if row["type"] == USER_SKILL then
        return {};
    end

    for _, field in ipairs({ "apply1", "apply2", "apply3", "apply4", "apply5", "apply6", }) do
        local ruleDesc = row[field];
        if (ruleDesc and ruleDesc ~= "") then
            local _, __, rule, para = string.find(ruleDesc, "([^%(]+)%((.*)%)$");
            local f = SkillM.queryRule(rule);
            local propInfo = f.parse(para);
            local propId = propInfo[1];

            if PropM.query(propId, "lictor") == 1 then
                table.insert(prop, f.parse(para));
            end
        end
    end

    local specialProps = FormulaM.invoke("GET_LICTOR_SPECIAL_PROPS", pet:getRank());
    for index = 1, #specialProps do
        table.insert(prop, specialProps[index]);
    end
    return prop;
end